package l2ft.gameserver.network.l2.s2c;

import java.util.List;

import l2ft.commons.collections.CollectionUtils;
import l2ft.gameserver.dao.MailDAO;
import l2ft.gameserver.model.Player;
import l2ft.gameserver.model.mail.Mail;
import l2ft.gameserver.network.l2.c2s.RequestExDeleteReceivedPost;
import l2ft.gameserver.network.l2.c2s.RequestExPostItemList;
import l2ft.gameserver.network.l2.c2s.RequestExRequestReceivedPost;
import l2ft.gameserver.network.l2.c2s.RequestExRequestReceivedPostList;


/**
 * ?????????? ??? ??????? ?? ?????? "?????" ??? "received mail", ???????? ??????
 * <br> ????? ?? {@link RequestExRequestReceivedPostList}.
 * <br> ??? ??????? ?? ?????? ? ?????? ?????? {@link RequestExRequestReceivedPost} ? ? ????? {@link ExReplyReceivedPost}.
 * <br> ??? ??????? ??????? ?????? ?????? {@link RequestExDeleteReceivedPost}.
 * <br> ??? ??????? ?????? send mail ?????? {@link RequestExPostItemList}.
 * @see ExShowSentPostList ??????????? ?????? ???????????? ?????
 */
public class ExShowReceivedPostList extends L2GameServerPacket
{
	private final List<Mail> mails;

	public ExShowReceivedPostList(Player cha)
	{
		mails = MailDAO.getInstance().getReceivedMailByOwnerId(cha.getObjectId());
		CollectionUtils.eqSort(mails);
	}

	// d dx[dSSddddddd]
	@Override
	protected void writeImpl()
	{
		writeEx(0xAA);
		writeD((int)(System.currentTimeMillis() / 1000L));
		writeD(mails.size()); // ?????????? ?????
		for(Mail mail : mails)
		{
			writeD(mail.getMessageId()); // ?????????? id ??????
			writeS(mail.getTopic()); // ?????
			writeS(mail.getSenderName()); // ???????????
			writeD(mail.isPayOnDelivery() ? 1 : 0); // ???? ??? 1 ?? ?????? ??????? ??????
			writeD(mail.getExpireTime()); // ????? ???????????????? ??????
			writeD(mail.isUnread() ? 1: 0); // ?????? ?? ????????? - ??? ?????? ??????? ? ??? ?????????? ????? ??????
			writeD(mail.getType() == Mail.SenderType.NORMAL ? 0 : 1); // returnable
			writeD(mail.getAttachments().isEmpty() ? 0 : 1); // 1 - ?????? ? ???????????, 0 - ?????? ??????
			writeD(0x00); // ???? ??? 1 ? ????????? ???????? 1 ?? ???????????? ????? "****", ???? ??? 2 ?? ????????? ???????? ????????????
			writeD(mail.getType().ordinal()); // 1 - ???????????? ???????? "**News Informer**"
			writeD(0x00);
		}
	}
}